package com.hc.acoustics.ui.activity

import android.content.Intent
import android.os.Bundle
import com.hc.acoustics.R
import com.hc.acoustics.base.BaseActivity
import com.hc.acoustics.util.FormulaUtil
import com.yc.yclibrary.base.YcAppCompatActivity
import kotlinx.android.synthetic.main.formula_button.*
import kotlinx.android.synthetic.main.type3_formula4_location_activity.*
import kotlinx.android.synthetic.main.type3_formula5_location_activity.*
import kotlinx.android.synthetic.main.type3_formula6_location_activity.*
import kotlinx.android.synthetic.main.type3_formula7_location_activity.*
import kotlinx.android.synthetic.main.type4_formula1_location_activity.*
import java.lang.NumberFormatException

/**
 *  限幅器 - 限幅器（已知电压增益）
 */
class Type4Formula1Activity : BaseActivity() {

    companion object {
        private const val KEY_TITLE = "key_title"
        @JvmStatic
        fun newInstance(activity: YcAppCompatActivity, title: String) {
            val intent = Intent(activity, Type4Formula1Activity::class.java)
            intent.putExtra(KEY_TITLE, title)
            activity.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.type4_formula1_location_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(intent.getStringExtra(KEY_TITLE))
        formulaResetBtn.setOnClickListener {
            formula4_1_a_TEV.reset()
            formula4_1_b_TEV.reset()
            formula4_1_P_TEV.reset()
            formula4_1_R_TEV.reset()
            formula4_1_f_TEV.reset()
            formula4_1_u_TEV.reset()
            formula4_1_x_TEV.reset()
            formula4_1_y_TEV.reset()
            formula4_1_z_TEV.reset()
        }
        formulaOkBtn.setOnClickListener {
            try {
                val resultB = FormulaUtil.formula4_1_1(formula4_1_a_TEV.getDouble())
                val resultU = FormulaUtil.formula4_1_2(
                    formula4_1_P_TEV.getDouble(),
                    formula4_1_R_TEV.getDouble()
                )
                val resultX = FormulaUtil.formula4_1_3(resultB, resultU)
                val resultY = FormulaUtil.formula4_1_4(formula4_1_f_TEV.getDouble())
                val resultZ = FormulaUtil.formula4_1_5(resultY)
                formula4_1_b_TEV.setText(resultB)
                formula4_1_u_TEV.setText(resultU)
                formula4_1_x_TEV.setText(resultX)
                formula4_1_y_TEV.setText(resultY)
                formula4_1_z_TEV.setText(resultZ)
            } catch (e: NumberFormatException) {
                formula4_1_b_TEV.setText("Infinity")
                formula4_1_u_TEV.setText("Infinity")
                formula4_1_x_TEV.setText("Infinity")
                formula4_1_y_TEV.setText("Infinity")
                formula4_1_z_TEV.setText("Infinity")
            }
        }
    }
}